//
//  BQSetViewController.m
//  BeeQuick
//
//  Created by 万伶童 on 16/11/21.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQSetViewController.h"
#import "BQLeaveUserCell.h"
#import "BQsetCell.h"
#import "BQCaCheCell.h"
#import "BQAboutMeVC.h"
#import "BQLogInVc.h"
static NSString * cellID = @"cell_id";
static NSString * setID = @"set_ID";
static NSString * leaveID = @"leave_id";
static NSString * caCheID = @"caChe_id";
@interface BQSetViewController ()<UITableViewDataSource,UITableViewDelegate,BQLeaveUserCellDelegate>
@property(weak,nonatomic)UITableView * tableView;
@end

@implementation BQSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //MARK: 接收通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(nightTime) name:@"night" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(defulTime) name:@"deful" object:nil];
    
    [self setupUI];
    //MARK:导航返回按钮 替换扫一扫
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"v2_goback"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonOF:)];
    self.navigationItem.leftBarButtonItem = barButtonItem;
    
}
// MARK:销毁
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"night" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"deful" object:nil];
    
}




//MARK: 返回上一个界面
- (void)buttonOF:(UIBarButtonItem *)batBtn{
    
    [self.navigationController popViewControllerAnimated:YES]; }

//MARK: 夜间模式通知方法
- (void)nightTime
{
    
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.2 alpha:1];
    
}

- (void)defulTime
{
    
    self.tableView.backgroundColor = [UIColor whiteColor];
}

- (void)setupUI
{
    UITableView * tabView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    tabView.dataSource = self;
    tabView.delegate = self;
    [tabView registerClass:[BQsetCell class] forCellReuseIdentifier:setID];
    [tabView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    [tabView registerClass:[BQCaCheCell class] forCellReuseIdentifier:caCheID];
    [tabView registerClass:[BQLeaveUserCell class] forCellReuseIdentifier:leaveID];
    
    // MARK:设置cell的间距
    tabView.sectionHeaderHeight = 5;
    tabView.sectionFooterHeight = 5;
    // cell最上面的头间距
    
    tabView.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0);
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    [self.view addSubview:tabView];
    // MRRK:关联
    self.tableView = tabView;
    
    

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 3;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0 && indexPath.row == 0)
    {
       BQsetCell* cell = [tableView dequeueReusableCellWithIdentifier:setID forIndexPath:indexPath];
        // cell选中效果
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        return cell;
    }
    else if (indexPath.section == 0 && indexPath.row == 2)
    {
       BQCaCheCell * cell = [tableView dequeueReusableCellWithIdentifier:caCheID forIndexPath:indexPath];
        // cell选中效果
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

    }
    if (indexPath.section == 1)
    {
        BQLeaveUserCell * cell = [tableView dequeueReusableCellWithIdentifier:leaveID forIndexPath:indexPath];
        // cell选中效果
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
 
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.textLabel.text= @"关于我们";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // cell选中效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;


}

//MARK: 关于我的cell跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0 && indexPath.row == 1)
    {
        
        BQAboutMeVC * aboutVC = [[BQAboutMeVC alloc]init];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
    
    if (indexPath.section == 0 && indexPath.row == 2) {
        
        
       UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"您确定要清除缓存吗" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            
        }]];
        
         [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            BQCaCheCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            cell.dataLabel.text = 0;
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    
    }
    
}

#pragma mark - 注销账号
- (void)pushToQuitView{
    
    
    //初始化AlertController
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定要退出当前的账号吗" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    
    //添加按钮
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //退出账号的点击事件
        [[NSNotificationCenter defaultCenter]postNotificationName:@"exitLogin" object:nil];
        
        
        [self.navigationController popViewControllerAnimated:YES];
        
     
        
    }]];
    
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //不作为
        
    }]];

    //模态切换显示表单
    [self presentViewController:alertController animated:YES completion:nil];
    
    
    NSLog(@"退出");
    
}


@end
